import 'package:enterprise_v6_app/app/components/custom_modele/custom_row_title.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_search_enter.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/dialog/custom_bottom_sheet.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/entities/closure/close_device_manger_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/closure/device_manger/door_control_controller.dart';
import 'package:enterprise_v6_app/generated/assets.dart';
import 'package:flutter/cupertino.dart';

/// 门禁/卡口
class DoorControlPage extends StatelessWidget {
  DoorControlPage({super.key});

  final controller = Get.put(DoorControlController());

  // 进入状态 0：正常 1：故障
  Widget _status(int? i) {
    switch (i) {
      case 0:
        return Container(
          padding: EdgeInsets.symmetric(vertical: 3.w, horizontal: 12.w),
          decoration: BoxDecoration(
              color: const Color(0xFF5C6DFF),
              borderRadius: BorderRadius.only(
                topLeft: Radius.circular(8.r),
                bottomRight: Radius.circular(8.r),
                topRight: Radius.circular(8.r),
              )),
          child: Text('正常',
              style: TextStyle(
                fontSize: 10.sp,
                color: Colors.white,
              )),
        );
      case 1:
        return Container(
          padding: EdgeInsets.symmetric(vertical: 3.w, horizontal: 12.w),
          decoration: BoxDecoration(
              color: const Color(0xFFFD6B6B),
              borderRadius: BorderRadius.only(
                topLeft: Radius.circular(8.r),
                bottomRight: Radius.circular(8.r),
                topRight: Radius.circular(8.r),
              )),
          child: Text('故障',
              style: TextStyle(
                fontSize: 10.sp,
                color: Colors.white,
              )),
        );
      case 2:
        return Container(
          padding: EdgeInsets.symmetric(vertical: 3.w, horizontal: 12.w),
          decoration: BoxDecoration(
              color: const Color(0xFFA5A9C8),
              borderRadius: BorderRadius.only(
                topLeft: Radius.circular(8.r),
                bottomRight: Radius.circular(8.r),
                topRight: Radius.circular(8.r),
              )),
          child: Text('离线',
              style: TextStyle(
                fontSize: 10.sp,
                color: Colors.white,
              )),
        );
      default:
        return const Text('');
    }
  }

  // 0：进 1：出 3：进/出
  String _inOrOut(int? i) {
    switch (i) {
      case 0:
        return '进';
      case 1:
        return '出';
      case 3:
        return '进/出';
      default:
        return '';
    }
  }

  // 设备类型 1:人员闸机 2:车辆闸机
  String _deviceType(String? i) {
    switch (i) {
      case '1':
        return '人员闸机';
      case '2':
        return '车辆闸机';
      default:
        return '';
    }
  }

  // 筛选状态
  _filterStatus() {
    CustomBottomSheet<Map<String, String>>(
      dataList: const [
        {'name': '正常', 'value': '0'},
        {'name': '故障', 'value': '1'},
      ],
      showValue: 'name',
      onConfirm: (value) {
        controller.personStatus = value['value'];
        controller.personStatusShow = value['name'];
        controller.update();
      },
    ).showBottomSheetDialog();
  }

  // 筛选设备类型
  _filterDeviceType() {
    CustomBottomSheet<Map<String, String>>(
      dataList: const [
        {'name': '人员闸机', 'value': '1'},
        {'name': '车辆闸机', 'value': '2'},
      ],
      showValue: 'name',
      onConfirm: (value) {
        controller.deviceType = value['value'];
        controller.deviceTypeShow = value['name'];
        controller.update();
      },
    ).showBottomSheetDialog();
  }

  // 筛选进出口
  _filterInOut() {
    CustomBottomSheet<Map<String, String>>(
      dataList: const [
        {'name': '进', 'value': '0'},
        {'name': '出', 'value': '1'},
        {'name': '进/出', 'value': '3'},
      ],
      showValue: 'name',
      onConfirm: (value) {
        controller.inOut = value['value'];
        controller.inOutShow = value['name'];
        controller.update();
      },
    ).showBottomSheetDialog();
  }

  /// 获取右侧筛选样式
  Widget _getFilterWidget() {
    return Container(
        padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 20.w),
        decoration: const BoxDecoration(
          gradient: LinearGradient(
            //渐变位置
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
            //渐变颜色[始点颜色, 结束颜色]
            colors: [
              Color(0xffE7EAFF),
              Colors.white,
              Colors.white,
              Colors.white,
            ],
          ),
        ),
        child: Column(
          children: [
            Expanded(
                child: SingleChildScrollView(
              child: Column(
                children: [
                  Align(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      '筛选',
                      style: TextStyle(
                        fontSize: 18.sp,
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                  ),
                  AppGaps.vGap20,
                  AppGaps.vGap16,
                  Align(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      '状态',
                      style: TextStyle(
                        fontSize: 14.sp,
                        fontWeight: FontWeight.w400,
                        color: AppColors.font_color_black,
                      ),
                    ),
                  ),
                  AppGaps.vGap16,
                  Container(
                    height: 38.w,
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(8.w),
                      border: Border.all(
                        color: AppColors.font_color_gray.withOpacity(0.3),
                        width: 1,
                      ),
                    ),
                    child: Row(
                      children: [
                        Expanded(
                          child: Container(
                            padding: EdgeInsets.symmetric(horizontal: 16.w),
                            child: Text(
                              controller.personStatusShow ?? '请选择状态',
                              style: TextStyle(
                                fontSize: 14.sp,
                                fontWeight: FontWeight.w400,
                                color: AppColors.font_color_black,
                              ),
                            ),
                          ),
                        ),
                        Container(
                          width: 38.w,
                          height: 38.w,
                          alignment: Alignment.center,
                          child: SvgPicture.asset(
                            Assets.imagesIconFilterRightArrow,
                            width: 18.w,
                            height: 16.w,
                          ),
                        ),
                      ],
                    ),
                  ).click(_filterStatus),
                  AppGaps.vGap16,

                  Align(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      '设备类型',
                      style: TextStyle(
                        fontSize: 14.sp,
                        fontWeight: FontWeight.w400,
                        color: AppColors.font_color_black,
                      ),
                    ),
                  ),
                  AppGaps.vGap16,
                  Container(
                    height: 38.w,
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(8.w),
                      border: Border.all(
                        color: AppColors.font_color_gray.withOpacity(0.3),
                        width: 1,
                      ),
                    ),
                    child: Row(
                      children: [
                        Expanded(
                          child: Container(
                            padding: EdgeInsets.symmetric(horizontal: 16.w),
                            child: Text(
                              controller.deviceTypeShow ?? '请选择类型',
                              style: TextStyle(
                                fontSize: 14.sp,
                                fontWeight: FontWeight.w400,
                                color: AppColors.font_color_black,
                              ),
                            ),
                          ),
                        ),
                        Container(
                          width: 38.w,
                          height: 38.w,
                          alignment: Alignment.center,
                          child: SvgPicture.asset(
                            Assets.imagesIconFilterRightArrow,
                            width: 18.w,
                            height: 16.w,
                          ),
                        ),
                      ],
                    ),
                  ).click(_filterDeviceType),

                  AppGaps.vGap16,
                  Align(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      '进出口',
                      style: TextStyle(
                        fontSize: 14.sp,
                        fontWeight: FontWeight.w400,
                        color: AppColors.font_color_black,
                      ),
                    ),
                  ),
                  AppGaps.vGap16,
                  Container(
                    height: 38.w,
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(8.w),
                      border: Border.all(
                        color: AppColors.font_color_gray.withOpacity(0.3),
                        width: 1,
                      ),
                    ),
                    child: Row(
                      children: [
                        Expanded(
                          child: Container(
                            padding: EdgeInsets.symmetric(horizontal: 16.w),
                            child: Text(
                              controller.inOutShow ?? '请选择进出口',
                              style: TextStyle(
                                fontSize: 14.sp,
                                fontWeight: FontWeight.w400,
                                color: AppColors.font_color_black,
                              ),
                            ),
                          ),
                        ),
                        Container(
                          width: 38.w,
                          height: 38.w,
                          alignment: Alignment.center,
                          child: SvgPicture.asset(
                            Assets.imagesIconFilterRightArrow,
                            width: 18.w,
                            height: 16.w,
                          ),
                        ),
                      ],
                    ),
                  ).click(_filterInOut),
                  AppGaps.vGap16,
                ],
              ),
            )),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                OutlinedButton(
                  onPressed: controller.reset,
                  child: Text(
                    '重置',
                    style: TextStyle(fontSize: 16.sp, color: AppColors.font_color_blue),
                  ),
                ),
                ElevatedButton(
                    style: ButtonStyle(backgroundColor: MaterialStateProperty.all(AppColors.font_color_blue)),
                    onPressed: controller.confirm,
                    child: Text(
                      '查询',
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 16.sp,
                      ),
                    )),
              ],
            ),
          ],
        ));
  }

  ///筛选
  Widget _screen() {
    return Container(
      width: 36.w,
      height: 32.w,
      alignment: Alignment.center,
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8.w)),
      child: SvgPicture.asset(
        AppImages.icon_search_screen,
        width: 18.w,
        height: 16.w,
      ),
    ).clickOnce(controller.screen);
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            key: controller.popScaffoldKeyCar,
            backgroundColor: AppColors.bg_color_grey,
            endDrawer: Container(
              width: AppDimens.sw - 80.w,
              height: AppDimens.sh,
              color: Colors.white,
              child: _getFilterWidget(),
            ),
            body: Container(
              padding: EdgeInsets.symmetric(
                horizontal: 16.w,
              ),
              child: Column(
                children: [
                  AppGaps.vGap14,
                  Row(
                    children: [
                      Expanded(
                        child: CustomSearchEnter(
                          height: 38.w,
                          hintText: '请输入设备名称',
                          text: controller.searchCarName,
                          onCallBack: (v) {
                            controller.searchCarName = v;
                            controller.confirm();
                          },
                        ),
                      ),
                      AppGaps.hGap8,
                      _screen(),
                    ],
                  ),
                  AppGaps.vGap16,
                  Expanded(
                      child: CustomEasyRefreshList<CloseDeviceMangerEntity>(
                    key: controller.globalKey,
                    dataLoad: controller.checkDeviceDetails,
                    itemBuilder: (item, index) {
                      return CustomTitleChartPage(
                        title: '${item.deviceName}',
                        titleRightWidget: _status(item.equipmentStatus),
                        child: Column(
                          children: [
                            CustomRowTitle(
                              title: '设备类型:',
                              content: _deviceType(item.equipmentType),
                            ),
                            CustomRowTitle(
                              title: '进口/出口:',
                              content: _inOrOut(item.inOrOut),
                            ),
                            CustomRowTitle(
                              title: '区域:',
                              content: '${item.regionName}',
                            ),
                            Container(
                              padding: EdgeInsets.only(left: 8.w),
                              child: Row(
                                children: [
                                  Text(
                                    '中控:',
                                    style: TextStyle(
                                      color: const Color(0xFFA0A7B5),
                                      fontSize: 13.sp,
                                    ),
                                  ),
                                  const Spacer(),
                                  CupertinoSwitch(
                                    activeColor: const Color(0xFF5C6DFF),
                                    value: item.whetherToInspect == 1 ? true : false,
                                    onChanged: (value) {
                                      controller.changeDeviceStatus(item);
                                    },
                                  ),
                                ],
                              ),
                            )
                          ],
                        ),
                      );
                    },
                  ))
                ],
              ),
            ),
          );
        });
  }
}
